Skip to content

fix(observability): six of seven alert rules could never fire - #416

Open
Matthew-Selvam wants to merge 2 commits into
Conway-Research:mainfrom
Matthew-Selvam:fix/alert-rules-read-unproduced-metrics
Open

Matthew-Selvam wants to merge 2 commits into
Conway-Research:mainfrom
Matthew-Selvam:fix/alert-rules-read-unproduced-metrics

Conversation

@Matthew-Selvam

Copy link
Copy Markdown

Summary

The default alert rules read ten metric names. Only two had a producer. Every absent name hit its rule's ?? 0 / ?? -1 fallback, so the condition returned false and the rule could never fire:

Metric Producer
balance_cents, survival_tier heartbeat/tasks.ts report_metrics
turns_last_hour, turns_total none
heartbeat_task_failures_total, heartbeat_task_successes_total none
policy_denies_total, policy_decisions_total none
inference_cost_cents none
unhealthy_child_count none
context_tokens_total none

Only balance_below_reserve and context_near_capacity were live. The alerting system was inert in production while its unit tests passed — those tests asserted only that the rules existed, never that they fire.

The fix

report_metrics now derives the missing metrics from tables the agent already writes — turns, policy_decisions, inference_costs, heartbeat_history, children. Each query is independent and individually wrapped, so one unavailable table degrades a single metric rather than failing the tick.

Gauges, not counters, deliberately. These are absolute DB totals recomputed each tick, so increment() would re-add the same total every tick and inflate it without bound. The six rules that read them via counters.get() were updated to gauges.get() to match.

Repinned existing tests

Four tests in observability.test.ts built snapshots with these names under counters. After the change they would pass vacuously — asserting a missing key produces 0. Repinned in the same change to publish them as gauges, so "does not fire" actually tests the threshold logic.

Deliberately not wired: context_tokens_total

This one measures context-window fill, which only the inference loop can observe. Reporting a constant would be worse than not reporting it, so the rule stays inert until someone instruments buildContextMessages. Flagged rather than guessed at — happy to do that as a follow-up if you want it.

Testing

src/__tests__/alert-rules-wiring.test.ts (new, 13 tests) asserts each rule fires against realistic values, that a healthy system fires nothing, that the policy rule respects its 10-sample floor, and that the derived SQL works against the real schema.

RED  (unfixed alerts.ts):  3 failed | 10 passed (13)
GREEN (with fix):          13 passed (13)

Verification

  • pnpm typecheck — clean
  • observability, alert-rules-wiring, heartbeat, heartbeat-scheduler, lifecycle — 181 passed

The default alert rules read ten metric names. Only two had a producer:

  balance_cents, survival_tier   <- set in heartbeat/tasks.ts report_metrics
  context_tokens_total            <- no producer
  heartbeat_task_failures_total   <- no producer
  heartbeat_task_successes_total  <- no producer
  policy_denies_total             <- no producer
  policy_decisions_total          <- no producer
  inference_cost_cents            <- no producer
  turns_last_hour                 <- no producer
  turns_total                     <- no producer
  unhealthy_child_count           <- no producer

Every absent name hit the rule's `?? 0` / `?? -1` fallback, so the condition
returned false and the rule could never fire. Only `balance_below_reserve` and
`context_near_capacity` were live. The alerting system was inert in production
while its unit tests passed, because those tests asserted only that the rules
EXISTED, never that they fire.

report_metrics now derives the missing metrics from tables the agent already
writes (turns, policy_decisions, inference_costs, heartbeat_history, children).
Each query is independent and individually wrapped, so an unavailable table
degrades one metric rather than failing the tick.

Publishing them as gauges rather than counters is deliberate: these are
absolute DB totals recomputed each tick, so `increment()` would re-add the same
total on every tick and inflate it without bound. The six rules that read them
via `counters.get()` were updated to `gauges.get()` to match.

Four existing tests in observability.test.ts constructed snapshots with these
names under `counters`, which would now pass vacuously. Repinned in this same
change to publish them as gauges, so "does not fire" really tests the threshold
logic instead of a missing key.

`context_tokens_total` is deliberately NOT wired: it measures the agent's
context-window fill, which only the inference loop can observe. Reporting a
constant would be worse than not reporting it. Left as-is, and the rule stays
inert until someone instruments buildContextMessages - flagged here rather than
guessed at.

New src/__tests__/alert-rules-wiring.test.ts (13 tests) asserts each rule FIRES
against realistic values, that a healthy system fires nothing, that the policy
rule respects its sample-size floor, and that the derived SQL works against the
real schema. RED check: 3 fail against the unfixed alerts.ts.

Verified: typecheck clean; 181 tests across observability, alert-rules-wiring,
heartbeat, heartbeat-scheduler and lifecycle pass.
The context_near_capacity alert rule reads context_tokens_total but no code
was publishing it. This renders the alert inert in production.

Instrument buildContextMessages to estimate and publish the current context
token count, enabling the alert to fire when context approaches budget limits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant